package controller;

import adapter.MessageAdapter;
import jdk.nashorn.internal.scripts.JS;
import listener.MessageListener;
import org.json.JSONArray;
import org.json.JSONObject;
import util.MyClient;
import view.FindPlayerPanel;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

/***
 * Author: Mr.Zhao, Time: 2018/5/6/16:24
 * Description: 寻找在线玩家面板的控制器
 */
public class FindPlayerController extends MessageAdapter {
    private FindPlayerPanel findPlayerPanel;
    private JButton btn_findPlayers;
    private JList<String> jList;
    private DefaultListModel<String> model;
    private JButton btn_invite;
    private JLabel myInfo;
    /**
     * 存放玩家信息的键值对，其键值为客户端与服务端通信时所能识别玩家的唯一标志
     * 可通过玩家信息获取到该玩家的唯一标识
     */
    private Map<String, String> plyersMap = new HashMap<String, String>();


    public FindPlayerController(FindPlayerPanel findPlayerPanel) {
        this.findPlayerPanel = findPlayerPanel;
        this.btn_findPlayers = findPlayerPanel.getBtn_findPlayers();
        this.btn_invite = findPlayerPanel.getBtn_invite();
        this.jList = findPlayerPanel.getjList();
        this.myInfo = findPlayerPanel.getMyInfo();
        this.model = new DefaultListModel<>();
        addListener();
    }

    private void addListener() {
        /* 为寻找玩家按钮添加监听事件 */
        btn_findPlayers.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(btn_findPlayers.getText() + "被点击");
                model.clear();
                jList.setModel(model);
                plyersMap.clear();
                MyClient client = MyClient.getInstance();
                JSONObject json = new JSONObject();
                json.put("flag", "列出在线玩家");
                client.sendMessage(json.toString());
            }
        });

        /* 为jList添加事件监听，如果选择的玩家可以被邀请，则将邀请按钮设置为可以点击 */
        jList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                String str = (String) jList.getSelectedValue();
                if (str != null && (str.contains("空闲") || str.contains("没有玩家在线"))) {
                    btn_invite.setEnabled(true);
                }
            }
        });

        /* 为邀请玩家按钮添加监听事件 */
        btn_invite.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str = (String) jList.getSelectedValue();
                String id = plyersMap.get(str);
                MyClient client = MyClient.getInstance();
                JSONObject json = new JSONObject();
                json.put("flag", "发起游戏邀请");
                json.put("to", id);
                client.sendMessage(json.toString());
            }
        });
    }

    /**
     * 显示在线玩家列表
     */
    @Override
    public void listPlayers(String jsonArrayStr) {
        JSONArray jsonArray = new JSONArray(jsonArrayStr);
        DefaultListModel<String> model = new DefaultListModel<>();
        if (jsonArray.length() == 0) {
            model.addElement("没有玩家在线");
            jList.setModel(model);
            return;
        }

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);
            String id = json.getString("id");
            boolean isBusy = json.getBoolean("busy");
            String busyStr = isBusy ? "已开局" : "空闲";
            String displayStr;
            displayStr = String.format("玩家%s在线，状态：%s", id, busyStr);
            model.addElement(displayStr);
            plyersMap.put(displayStr, id);
        }
        //清除元素后需重新设置
        jList.setModel(model);
    }

    @Override
    public void myInfo(String id) {
        this.myInfo.setText("我的ID：" + id);
        this.myInfo.repaint();
    }
}
